<?php

class CarrerasController extends Zend_Controller_Action
{

    protected $_carreras = null;

    public function init()
    {
        /* Initialize action controller here */
        $this->view->baseUrl = $this->_request->getBaseUrl();
        $this->view->cliente = Zend_Auth::getInstance()->getIdentity();
        $this->_carreras = new Application_Model_DbTable_Carreras();
        $this->view->titulo = "Carreras";
    }

    public function indexAction()
    {
        // action body
        $this->view->carreras = $this->_carreras->obtenerTodo();
    }

    public function crearAction()
    {
        // action body
        $form = new Application_Form_Carrera();
        
        if( $this->getRequest()->isPost() ){
            
            $img_elmnt = $form->getElement('imagen');
            $art_id = $this->_carreras->obtenerSigId();
            $img_elmnt->setDestination($this->_carreras->crearCarpeta($art_id));

            if( $form->isValid( $this->_getAllParams() ) ) {
                if ($form->imagen->receive()){ 
                    $nom = $form->getValue('nombre');
                    $des = $form->getValue('descripcion');
                    $fecha = $form->getValue('fecha_inicio');
                    $horario = $form->getValue('horario');
                    $dur = $form->getValue('duracion');
                    $costo = $form->getValue('costo');
                    $id = $this->_carreras->crearCarrera($nom, $des, $fecha, $horario, $dur, $costo);
                    $imagen = $form->getValue('imagen');
                    $this->_carreras->guardarImagen($id,$imagen);
                    $this->_redirect('/carreras');
                }
            }    
            $this->view->form = $form;    
        }elseif( $this->getRequest()->isGet() ){
            $this->view->form = $form;
        }
        
    }

    public function editarAction()
    {
        //http://danielbonhaure/carreras/editar/id/#
        $id = $this->_getParam('id');
        $carrera = $this->_carreras->obtenerCarrera($id);
        $this->view->carrera = $carrera;
        
        $form = new Application_Form_Carrera();
        $form->setAction('/carreras/editar/id/' . $id);
        $form->setMethod('post');
        $submit = $form->getElement('Crear');
        $submit->setLabel('Guardar');
        $form->inicializar($carrera);
        $imagen = $form->getElement('imagen');
        $upload = Zend_Registry::get('upload_paths');
        $imagen->setDestination($upload->paths->upload->images->carreras . "/" . $carrera->id);
        
        if( $this->getRequest()->isPost() ){
          
            if ($form->isValid( $this->_getAllParams() )) {
                if ($form->imagen->receive()){ 
                    $data = array(
                        'nombre' => $form->getValue('nombre'),
                        'descripcion' => $form->getValue('descripcion'),
                        'imagen' => $form->getValue('imagen'),
                        'path' => $imagen->getDestination(),
                        'fecha' => $form->getValue('fecha_inicio'),   
                        'horario' => $form->getValue('horario'),   
                        'duracion' => $form->getValue('duracion'),   
                        'costo' => $form->getValue('costo'),   
                    );
                    $this->_carreras->actualizarCarrera($id,$data);
                    $this->_redirect('/carreras');  
                }
            }

            $this->view->form = $form;
            
        }elseif( $this->getRequest()->isGet() ){
            
            $this->view->form = $form;
        }    
        
    }

    public function borrarAction()
    {
        //http://danielbonhaure/carreras/borrar/id/*
        $role = Zend_Auth::getInstance()->getIdentity()->role;
        
        if ($this->getRequest()->isPost()) {
            $id = $this->getRequest()->getPost('id');
            $borrar = $this->getRequest()->getPost('borrar');
            if ($borrar == 'Si') {
                if ($role == "admin" || $role == "adminCarreras") {
                    $this->_carreras->borrarCarrera($id);
                    $this->_redirect('/carreras'); 
                } else {
                    $this->_redirect('/error/permiso');  
                }
            } elseif ($borrar == "No") {
                $this->_redirect('/carreras'); 
            }
        } else {
            $id = $this->_getParam('id');
            if ($role == "admin" || $role == "adminCarreras") {
                $this->view->carrera = $this->_carreras->obtenerCarrera($id);
            } else {
                $this->_redirect('/error/permiso');  
            }
        }   
    }


}







